package com.xo.xodemo.controller.system;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xo.xodemo.common.Result;
import com.xo.xodemo.mapper.db1.PositionMapper;
import com.xo.xodemo.mapper.db1.RoleMapper;
import com.xo.xodemo.mapper.db1.UserMapper;
import com.xo.xodemo.model.Position;
import com.xo.xodemo.model.Role;
import com.xo.xodemo.model.RoleForTransfer;
import com.xo.xodemo.model.TransferData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/position")
public class PositionController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    RoleMapper roleMapper;
    @RequestMapping("add")
    @ResponseBody
    //建立职位
    public Result add(Model model,Position position) {
    //一.判断新增职位名称是否存在
        //1.通过职位名称查询
        Position positionByName = positionMapper.findPositonByName(position.getPositionName());
        if (positionByName != null){
            return  Result.error("职位名已被使用");
        }
        else {
            int result = positionMapper.addPosition(position.getPositionName());
            if (result == 1){
                return  Result.success();
            }else {
                return  Result.error("职位添加失败");
            }

        }


    }
    @RequestMapping("delete")
    @ResponseBody
    public String delete(Model model) {

        return  "show";

    }
    @RequestMapping("update")
    @ResponseBody
    public String update(String userName,String password,String positionId) {


        return  "show";

    }
    //返回职位列表
    @RequestMapping("queryposition")
    @ResponseBody
    public Result queryposition(@Param("page") Integer page, @Param("limit")Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo pageInfo=new PageInfo(positionMapper.findAll());
        return  Result.success(pageInfo);

    }
    //返回职位列表给下拉框用
    @RequestMapping("queryposition1")
    @ResponseBody
    public Result queryposition1() {

        return  Result.success( positionMapper.findAll());

    }
    @RequestMapping("queryrolepage")
    public String queryall(Model model, @RequestParam("positionId") Integer positionId) {
        model.addAttribute("positionId",positionId);
        return  "system/positiondetailpage";

    }
    @RequestMapping("query")
    @ResponseBody
    public Result query(@RequestParam("positionId") Integer positionId) {
        TransferData transferData=new TransferData();
        List<RoleForTransfer> lft=new ArrayList<>();
         List <Role> lr=  roleMapper.findAll();
        List <Integer> lt=  new ArrayList<>();
        List <Role> lr1=  roleMapper.findRoleByUserPosition(positionId);
         //把所有权限赋值进去
         for(Role r:lr){
            RoleForTransfer rf=new RoleForTransfer();
            rf.setTitle(r.getRoleDescription());
            rf.setValue(r.getRoleId());
            lft.add(rf);
         }
         for(Role r:lr1){
           lt.add(r.getRoleId());
         }
         //把用户目前具有的权限id加进去
         transferData.setPositionhasrole(lt);
        transferData.setTdlist(lft);
        return  Result.success(transferData);
    }
    @RequestMapping("addpositionrole")
    @ResponseBody
    public Result addpositionrole(@RequestParam("positionId") String positionId,@RequestParam("roleList") List<String> roleList) {
         int i= positionMapper.addPositionRole(roleList,positionId);
        return  Result.success(i);

    }
    @RequestMapping("deletepositionrole")
    @ResponseBody
    public Result deletepositionrole(@RequestParam("positionId") String positionId,@RequestParam("roleList") List<String>roleList) {
        int i= positionMapper.deletePositionRole(roleList,positionId);
        return  Result.success(i);
    }


    //删除角色
    @RequestMapping("deleteOnePosition")
    @ResponseBody
    public Result deleteOnePosition(@RequestParam("positionId") Integer positionId) {
        int i= positionMapper.deleteOnePosition(positionId);
        return  Result.success(i);
    }
}
